package cn.wolfcode.web.controller;

import cn.wolfcode.qo.ConsumptionReportQueryObject;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.IConsumptionReportService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/consumptionReport")
public class ConsumptionReportController {
    @Autowired
    private IConsumptionReportService consumptionReportService;

    @Autowired
    private IBusinessService businessService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") ConsumptionReportQueryObject qo) {
        model.addAttribute("pageInfo", consumptionReportService.query(qo));
        model.addAttribute("businesses", businessService.listAll());
        return "consumptionReport/list";
    }

    @RequestMapping("/listBar")
    public String listBar(Model model, @ModelAttribute("qo") ConsumptionReportQueryObject qo) {

        // List<Map<String,Object>> mapList = consumptionReportService.queryByChart(qo.getGroupType());
        qo.setPageSize(Integer.MAX_VALUE);
        PageInfo<Map<String, Object>> query = consumptionReportService.query(qo);
        // x轴
        List<Object> groupTypeList = new ArrayList<>();
        // 总消费金额
        List<Object> totalAmountList = new ArrayList<>();
        // 总实收金额
        List<Object> payAmountList = new ArrayList<>();
        // 总优惠金额
        List<Object> discountAmountList = new ArrayList<>();
        // 总订单数
        List<Object> totalCountList = new ArrayList<>();
        for (Map<String, Object> map : query.getList()) {
            groupTypeList.add(map.get("groupType"));
            totalAmountList.add(map.get("totalAmount"));
            payAmountList.add(map.get("payAmount"));
            discountAmountList.add(map.get("discountAmount"));
            totalCountList.add(map.get("totalCount"));
        }
        model.addAttribute("groupTypeList", JSON.toJSONString(groupTypeList));
        model.addAttribute("totalAmountList", JSON.toJSONString(totalAmountList));
        model.addAttribute("payAmountList", JSON.toJSONString(payAmountList));
        model.addAttribute("discountAmountList", JSON.toJSONString(discountAmountList));
        model.addAttribute("totalCountList", JSON.toJSONString(totalCountList));;
        System.out.println(JSON.toJSONString(groupTypeList));
        return "consumptionReport/listBar";
    }

}
